﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Automation;

namespace SilverlightOracle
{
    public class ScollDataGrid : DataGrid
    {
        protected override void OnMouseWheel(MouseWheelEventArgs e)
        {
            base.OnMouseWheel(e);
            //Try getting the automationpeer from this datagrid
            AutomationPeer automationPeer = FrameworkElementAutomationPeer.FromElement(this);
            if (automationPeer == null)
            {
                //if not try creating a new one for this datagrid
                automationPeer = FrameworkElementAutomationPeer.CreatePeerForElement(this);
            }
            //Try getting the scroll provider for this datagrid
            IScrollProvider scrollProvider = automationPeer.GetPattern(PatternInterface.Scroll) as IScrollProvider;
            if (scrollProvider != null)
            {
                ScrollAmount scrollAmount = ScrollAmount.NoAmount;
                if (e.Delta < 0)
                    scrollAmount = ScrollAmount.SmallIncrement;//for scroll down
                else if (e.Delta > 0)
                    scrollAmount = ScrollAmount.SmallDecrement;//for scoll up


                if (scrollProvider.VerticallyScrollable)
                {
                    scrollProvider.Scroll(ScrollAmount.NoAmount, scrollAmount);
                }
            }
        }
    }
}
